﻿using AxMSTSCLib;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace RDPManager
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            Rdps_View_Init();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Top = 0;
            Left = 0;
            Width = Screen.PrimaryScreen.WorkingArea.Width;
            Height = Screen.PrimaryScreen.WorkingArea.Height;
        }

        const int LEADING_SPACE = 12;
        const int CLOSE_AREA = 15;

        // 页签画关闭按钮
        private void Rdp_Tabs_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - CLOSE_AREA, e.Bounds.Top + 4);
            e.Graphics.DrawString(Rdps_Tab.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + LEADING_SPACE, e.Bounds.Top + 4);
            e.DrawFocusRectangle();
        }

        // 关闭按钮触发
        private void Rdp_Tabs_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            for (int i = 0; i < Rdps_Tab.TabPages.Count; i++)
            {
                Rectangle r = Rdps_Tab.GetTabRect(i);
                //Getting the position of the "x" mark.
                Rectangle closeButton = new Rectangle(r.Right - CLOSE_AREA, r.Top + 4, 9, 7);
                if (closeButton.Contains(e.Location))
                {
                    TabPage tabPage = Rdps_Tab.TabPages[i];
                    if (tabPage.Controls.Count > 0)
                    {
                        foreach (Control control in tabPage.Controls)
                        {
                            if (control is AxMsRdpClient8NotSafeForScripting)
                            {
                                AxMsRdpClient8NotSafeForScripting client = (AxMsRdpClient8NotSafeForScripting)control;
                                if (client.Connected == 1)
                                {
                                    client.Disconnect();
                                }
                            }
                        }
                    }
                    Rdps_Tab.TabPages.RemoveAt(i);
                    break;
                }
            }
        }

        // 远程列表初始化
        private void Rdps_View_Init()
        {
            ConfigLoader.Load();
            Rdps_View.Nodes.Clear();
            RdpConfig mainConfig = ConfigLoader.Main_config;
            foreach (RdpConfig rdpConfig in mainConfig.Dirs)
            {
                TreeNode child = new TreeNode(rdpConfig.Name);
                rdpConfig.write(child);
                Rdps_View.Nodes.Add(child);
            }
            foreach (SingleConfig singleConfig in mainConfig.Singles)
            {
                TreeNode child = new TreeNode(singleConfig.Name + "(" + singleConfig.IP + ")");
                child.Tag = singleConfig;
                Rdps_View.Nodes.Add(child);
            }
        }

        // 远程列表双击连接
        private void Rdps_View_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                return;
            }
            SingleConfig singleConfig = (SingleConfig)e.Node.Tag;
            RdpClient client = new RdpClient(Rdps_Tab, singleConfig);
            if (!client.TryConnect())
            {
                Rdps_Tab.Controls.Remove(client.Page);
            }
        }

        // 点击重新加载远程列表
        private void M1L1_Reload_Click(object sender, EventArgs e)
        {
            Rdps_View_Init();
            MessageBox.Show("重载成功!");
        }

        private void M1L2_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}